//
//  AppDelegate.swift
//  onemeetingmac
//
//  Created by 张衡 on 2019/12/11.
//  Copyright © 2019 onemeeting. All rights reserved.
//

import Cocoa

//@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    
    //使用NSStatusItem应该作为全局变量，而不是局部变量，如果作为局部变量，将没有效果。
    var status = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
    var customMenu = NSMenu()
    
    //网页回调参数保存
    weak var urlDelegate: WebSchemaDelegate?
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        //ThemeManager.darkTheme.apply()
        self.showScreenRecordingPrompt()
        self.addStatusBar()
        self.load_UserInfoManager()
        self.rootViewController()
        self.autoCheckVersion()
    }
    
    func rootViewController() {
        self.windowController.showWindow(nil)
    }
    
    lazy var windowController: NSWindowController = {
        let window = NSWindow.init(contentViewController: self.controller)
        let controller = NSWindowController.init(window: window)
        return controller
    }()
    
    lazy var controller: MainViewController = {
        let view = MainViewController.init()
        return view
    }()
    
    
    func autoCheckVersion() {
        if UserInfoManager.shared.noAutoCheck == false {
            Mmanager.versionCompUpdate()
        }
        Mmanager.checkAnnouce()
    }
    
       
    func load_UserInfoManager() {
        UserInfoManager.shared.lang = UserInfoManager.shared.lang ?? "zh-Hans"
        UserInfoManager.shared.isAudioVolume = UserInfoManager.shared.isAudioVolume ?? false
        UserInfoManager.shared.noAutoCheck = UserInfoManager.shared.noAutoCheck ?? false
        UserInfoManager.shared.isNETTip = UserInfoManager.shared.isNETTip ?? false
        UserInfoManager.shared.scene = UserInfoManager.shared.scene ?? 0
        UserInfoManager.shared.name = Td.sh().name
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }

    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        ///NSApp.windows.first?.makeKeyAndOrderFront(nil)
        return true
    }
    
    func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {
        let menu = NSMenu.init(title: "")
        let itme1 = NSMenuItem.init(title: "其他", action: nil, keyEquivalent: "k")
        itme1.target = self
        let submune = NSMenu.init(title: "subMenu")
        let itme2 = NSMenuItem.init(title: "离开会议", action: nil, keyEquivalent: "g")
        itme2.target = self
        submune.addItem(itme2)
        menu.addItem(itme1)
        menu.setSubmenu(submune, for: itme1)
        return menu
    }
    
    
    func showScreenRecordingPrompt() {
        if #available(OSX 10.15, *) {
            CGWindowListCreateImage(CGRect.init(x: 0, y: 0, width: 1, height: 1), CGWindowListOption.optionOnScreenOnly, kCGNullWindowID, CGWindowImageOption.boundsIgnoreFraming)
        }
    }
    
    /*
    func canRecordScreen() -> Bool {
        if #available(OSX 10.15, *) {
            let windowList = CGWindowListCopyWindowInfo(CGWindowListOption.optionOnScreenOnly, kCGNullWindowID)
            let numberOfWindows = CFArrayGetCount(windowList)
            var numberOfWindowsWithName = 0
            for idx in 0..<numberOfWindows {
                let windowInfo = CFArrayGetValueAtIndex(windowList, idx)
                
                let windowName = windowInfo![kCGWindowName]
                if (windowName != nil) {
                    numberOfWindowsWithName+=1
                }else {
                    break
                }
            }
            return Int(numberOfWindows) == numberOfWindowsWithName
        }
        return true
    }
    */

}




extension AppDelegate {
    
    @objc func checkVersion() {
        let window = NSWindow.init(contentViewController: URLoadViewContoller.init())
        window.lorderFront(nil,makeKey: false)
    }
    
    @objc func about() {
        let window = NSWindow.init(contentViewController: VersionController.init())
        window.lorderFront(nil,makeKey: false)
    }
    
    @objc func clickPreferences() {
        TrtcEngine.showPreferences(onWindow:nil)
    }
    
    func addStatusBar() {
        //设置点击状态图标时的响应事件
        self.status.target = self
        self.status.action = #selector(statusBarClicked(sender:))
        //设置状态图标
        self.status.image = NSImage.init(named: "icon_statusbar")
        //设置应用图标
        //NSApp.applicationIconImage = NSImage.init(named: "icon_meeting_audio_mute")
        //NSMenu
        self.customMenu.title = "Title"
        self.customMenu.addItem(withTitle: "召开新的会议", action: #selector(statusBarClicked(sender:)), keyEquivalent: "k")
        self.customMenu.addItem(withTitle: "加入会议", action: #selector(statusBarClicked(sender:)), keyEquivalent: "g")
        self.customMenu.addItem(withTitle: "结束会议", action: #selector(statusBarClicked(sender:)), keyEquivalent: "d")
        //self.customMenu.addItem(withTitle: "退出", action: #selector(NSApp.terminate(_:)), keyEquivalent: "q")
        self.status.menu = self.customMenu
    }
    
    @objc func statusBarClicked(sender:NSStatusItem)  {
        
    }
}
